﻿@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
 ------------------------------------------------------------*@

@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.OpcUa.Api.Core.Models;

@inject NavigationManager NavigationManager
@inject Services.Browser BrowseManager

<div class="drawer-content list-group">
    <div class="list-group-item text-justify list-group-item-heading">
        <b>Select an action.</b>
        <br>
        <div class="dropdown">
            <div class="select">
                <select name="dropdown" @onchange="((eventArgs) => SelectActionAsync(NodeData.Id, eventArgs))">
                    <option value="@ActionType.Nothing">Select an action</option>
                    @if (NodeData.NodeClass == NodeClass.Variable)
                    {
                        if (NodeData.AccessLevel.HasFlag(NodeAccessLevel.CurrentRead))
                        {
                            <option value="@ActionType.Read">@ActionType.Read.ToString()</option>
                        }
                        if (NodeData.AccessLevel.HasFlag(NodeAccessLevel.CurrentWrite))
                        {
                            <option value="@ActionType.Write">@ActionType.Write.ToString()</option>
                        }
                    }
                    else if (NodeData.NodeClass == NodeClass.Method)
                    {
                        <option value="@ActionType.Call">@ActionType.Call.ToString()</option>
                    }
                </select>
            </div>
        </div>
    </div>
    <hr />
    <div class="list-group-item text-left">
        @if (TypeOfAction == ActionType.Read)
        {
            <div class="@ResponseClass">
                <span>value = </span>
                <span>@Response</span>
                <div class="reload">
                    <a class="oi oi-reload" href="javascript: void(0)" @onclick="@(() => ReadAsync(NodeData.Id))"></a>
                </div>
            </div>
        }
        else if (TypeOfAction == ActionType.Write)
        {
            <form>
                <div class="form-group">
                    <label>Enter value</label>
                    <input @bind="@Value" aria-label="value" type="text" class="form-control" />
                </div>
                <hr />
                <div>
                    <button @onclick="@(() => WriteAsync(NodeData.Id, Value))" type="button" class="btn btn-primary shadow-none">Apply</button>
                </div>
            </form>
            <div class="@ResponseClass">@Response</div>
        }
        else if (TypeOfAction == ActionType.Call)
        {
            @if (_parameters?.InputArguments != null)
            {
                <form>
                    @{int index = 0;}
                    @foreach (var parameter in _parameters.InputArguments)
                    {
                        //I need the local variable otherwise the bind ValueArray[index] has always the value at the end of the loop
                        int i = index;
                        <div class="form-group">
                            <label>@parameter.Type.DisplayName</label>
                            <input @bind="@Values[i]" aria-label="value" type="text" class="form-control" placeholder="@parameter.Type.Description" />
                        </div>
                        index++;
                    }
                </form>
             }
            <hr />
            <div>
                <button @onclick="@(async() => await CallAsync(NodeData.Id, Values))" type="button" class="btn btn-primary shadow-none">Apply</button>
            </div>
            <div class="@ResponseClass">
                @if (BrowseManager.MethodCallResponse != null)
                {
                    if (BrowseManager.MethodCallResponse.Results.Count > 0)
                    {
                        <div>response = </div>
                        foreach (var response in BrowseManager.MethodCallResponse.Results)
                        {
                            <div>@response.Value (@response.DataType)</div>
                        }
                    }
                    else
                    {
                        <div>method call successfully executed</div>
                    }
                }
                else
                {
                    <div>method call failed</div>
                    <div>@Response</div>
                }
            </div>
        }
    </div>
</div>